<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Nouns</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="classics.css" rel="stylesheet" type="text/css">
</head>
<?php

try {
	$db = new PDO('sqlite:Latin.db3');
	$q = $db->query('SELECT DISTINCT noun FROM nouns;');
	$tmp = $q->fetchAll();
	$nom = $tmp[array_rand($tmp)]['noun'];
	
	$q = $db->query("SELECT * FROM nouns WHERE noun='$nom';");

	$numbers = array();
	foreach( $q->fetchAll() as $row) {
		$noun[$row['number']] = $row;
		$numbers[$row['number']] = 1;
	}
	
	$tmp = "";
	$vtab = "<caption><b>".$nom."</b>&nbsp;&nbsp;&nbsp;<i>".$noun['singular']['gender']."</i> ".$noun['singular']['meaning']."</caption>\n";
	foreach(array('singular', 'plural') as $number) {
		if(isset($numbers[$number])) {
			$tmp .= "<th align=\"center\">$number</th>";
		}
	}
	$vtab = $vtab . "<tr class=\"separate-above\"><td></td>$tmp</tr>\n";

	foreach(array('nominative', 'genitive', 'dative', 'accusative', 'ablative', 'vocative') as $case) {
		$isblank = True;
		foreach($noun as $number => $aNum) {
			if (mb_strlen($aNum[$case], 'UTF8') > 0) {
				$isblank = False;
				break;
			}
		}
		
		if ($isblank) {
			continue;
		}
		
		$vtab = $vtab . "<tr><th>$case</th>";
		foreach(array('singular', 'plural') as $number) {
			$vtab = $vtab . "<td align=\"center\">";
			$tmp = explode("|", $noun[$number][$case]);
			foreach($tmp as $wd){
				if (mb_strlen($wd, 'UTF8') > 0) {
					$txts[] = $wd;
					$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\">";
				}
			}
			$vtab = $vtab . "</td>";
		}
		$vtab = $vtab . "</tr>\n";
	}

	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>


<script type="text/javascript">
function check() 
{

<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "if (document.n01.text".$i.".value=='".$txts[$i]."')\n{\n";
		print "\tdocument.n01.text".$i.".disabled=true;\n}\n";
	}
?>

}

function newWord() 
{
<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "document.n01.text".$i.".disabled=false;\n";
 		print "document.n01.text".$i.".value='';\n";
	}
?>
	location.reload();
}
</script>
<body>


<div id="container">
<div id="center">
<form name="n01" action="">
<table align="center">
<?php
	print $vtab;
?>
<tr class="separate-above">
	<td align="center" colspan=<?php print count($numbers); ?>><input name="Check" onclick="check()" value="Check" type="button"></td>
	<td align="center"><input name="New Word" onclick="newWord()" value="New Word" type="button"></td>
</tr>
</table>
</form>
</div>
</div>

</body>
</html>
